import { defineStore } from "pinia";
import { computed, reactive, ref, toRefs } from "vue";
import apis from '@/apis/index';

/**
 * 考勤信息-页面-共享数据仓库
 */
export const useAttendanceInfoStore = defineStore("attendanceInfo", () => {
  const attendanceInfoListRef = ref([
    {
      attendanceId: 1,
      attendanceType: 1,
      staffInfo: {
        staffId: 34,
        staffName: '谢霞',
        departmentInfo: {
          departmentId: 1,
          departmentName: '人事部'
        }
      },
      date: '2022-10-09 07:14:25',
    },
    {
      attendanceId: 2,
      attendanceType: 2,
      staffInfo: {
        staffId: 35,
        staffName: '小明',
        departmentInfo: {
          departmentId: 1,
          departmentName: '人事部'
        }
      },
      date: '2022-10-09 07:14:25',
    },
    {
      attendanceId: 3,
      attendanceType: 2,
      staffInfo: {
        staffId: 36,
        staffName: '王明',
        departmentInfo: {
          departmentId: 2,
          departmentName: '研发部'
        }
      },
      date: '2022-10-08 07:14:25',
    },
    {
      attendanceId: 4,
      attendanceType: 3,
      staffInfo: {
        staffId: 37,
        staffName: '吴霞',
        departmentInfo: {
          departmentId: 3,
          departmentName: '运营部'
        }
      },
      date: '2022-10-02 07:14:25',
    },
    {
      attendanceId: 5,
      attendanceType: 2,
      staffInfo: {
        staffId: 35,
        staffName: '龙伟',
        departmentInfo: {
          departmentId: 5,
          departmentName: '销售部'
        }
      },
      date: '2022-10-11 07:14:25',
    }
  ]);     // 职级列表
  const totalRef = ref(0);     // 列表长度
  const attendanceInfoDetailRef = ref(null);    // 职级详情
  const isLoadingRef = ref(false);   // 是否正在加载
  const isClosedRef = ref(false);   // 是否处于关闭状态
//   const isAttendanceLoadingRef = ref(true);
  
  // 更新职级列表
  async function updateAttendanceInfoList(data:any) {
    attendanceInfoListRef.value = data;
    totalRef.value = data.length;
  }

  // 更新职级详情
  async function updateAttendanceInfoDetail(data:any) {
    attendanceInfoDetailRef.value = data;
  }

  // 设置搜索栏状态
  function setIsClosed(bool: boolean) {
    isClosedRef.value = bool;
  }

  // 设置加载状态
  function setIsLoading(bool: boolean, delay: number = 500) {
    // setTimeout(() => {
      isLoadingRef.value = bool;
    // }, delay);
  }

  // 初始化职级列表
  async function initAttendanceInfoList(page = 1, size = 10, queryData = {}) {
    try {
      const params = {
        page,
        size,
        queryData,
      }
      const data = await apis.attendanceInfo.getAttendanceInfoList_API(params);
      console.log(data,'AttendanceInfo-list');
      if (data) {
        // 更新数据
        await updateAttendanceInfoList(data);
        console.log(attendanceInfoListRef,'AttendanceInfoListRef')
        setIsLoading(false);
        // 修改加载状态
        // setTimeout(() => {
        //   isAttendanceLoadingRef.value = false;
        //   setIsLoading(false);
        // }, 1500);
      }
    } catch (error) {
      console.log(error, 'pinia获取奖惩记录列表数据失败');
      return;
    }
  }

  // 获取职级详情
  async function getAttendanceInfoDetail() {
    try {
      const data = await apis.attendanceInfo.getAttendanceInfoDetail_API();
      console.log(data,'level-detail');
      if (data) {
        // 更新数据
        await updateAttendanceInfoDetail(data);
        console.log(attendanceInfoDetailRef,'levelDetailRef')
        // 修改加载状态
        // setTimeout(() => {
        //   isAttendanceLoadingRef.value = false;
        // }, 1500);
      }
    } catch (error) {
      console.log(error, 'pinia获取奖惩记录详情数据失败');
      return;
    }
  }

  return {
    isClosedRef,
    setIsClosed,
    isLoadingRef,
    setIsLoading,
    attendanceInfoListRef,
    attendanceInfoDetailRef,
    totalRef,
    // isAttendanceLoadingRef,
    initAttendanceInfoList,
    getAttendanceInfoDetail
  }
});